package com.lagou.work;

import com.lagou.work.zookeeper.registry.RpcRegistryHandler;
import com.lagou.work.zookeeper.registry.impl.RpcRegistryHandlerImpl;

import java.util.List;

public class ConsumeBoot {

    // 参数定义
//    private static final String PROVIDE_NAME = "UserService#sayHello#";

    public static void main(String[] args) throws InterruptedException {
        start();
    }

    public static void start() throws InterruptedException {
        while (true) {
            RpcRegistryHandlerImpl rpcRegistryHandler = new RpcRegistryHandlerImpl();
            List<String> discoveryList = rpcRegistryHandler.getChildrenList();
            if (discoveryList == null || discoveryList.size() == 0) {
                discoveryList = rpcRegistryHandler.discovery(UserService.class.getName());
            }
            for (String discovery : discoveryList) {
                String[] split = discovery.split(":");
                UserService userService = (UserService) new RpcConsumer().createProxy(UserService.class, split[0], Integer.parseInt(split[1]));

                String result = userService.sayHallow("我是客户端"+ discovery);
                System.out.println("服务端"+discovery+"返回：" + result);
                Thread.sleep(2000);
            }
        }
    }

}
